package com.rentcars.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rentcars.entity.CarDevice;
import com.rentcars.entity.CarPrice;
import com.rentcars.entity.Cars;
import com.rentcars.entity.Result;
import com.rentcars.mapper.CarDeviceMapper;
import com.rentcars.mapper.CarPriceMapper;
import com.rentcars.service.CarDeviceService;
import com.rentcars.service.CarPriceService;
import com.rentcars.service.CarShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zezhao
 * 新车上架
 */
@RestController
@RequestMapping("/carsup")
public class CarUpController {
    @Autowired
    private CarShareService carShareService;

    @Autowired
    private CarDeviceService carDeviceService;
    @Autowired
    private CarPriceService carPriceService;

    @RequestMapping("")
    public ModelAndView toAdd(){//左侧跳转页面
        return new ModelAndView("putaway-list");
    }

    @RequestMapping("/query")
    public PageInfo<Cars> query(@RequestParam(required = false) String carcode,
                                @RequestParam(required = false) String mobile,
                                @RequestParam(required = false) Integer pageNo,
                                @RequestParam(required = false) Integer pageSize){

        //查询所有数据
        PageInfo<Cars> carsPageInfo = carShareService.searchPutawayCars(carcode,mobile, pageNo, pageSize);
        return carsPageInfo;
    }

    @RequestMapping("/edit")
    public ModelAndView edit(String carid){//修改
        Cars cars=carShareService.selectByPrimaryKey2(carid);

        System.out.println(cars);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("car",cars);
        modelAndView.setViewName("putaway-edit");
        return modelAndView;
    }

    //修改
    @RequestMapping("/upEdit")
    public Result upEdit(@RequestParam(required = false) String carid,
                         @RequestParam(required = false) Double dayprice,
                         @RequestParam(required = false) Double tempmoney,
                         @RequestParam(required = false) Double rulemoney,
                         @RequestParam(required = false) Double basicinsure,
                         @RequestParam(required = false) String deviceid,
                         @RequestParam(required = false) String simcard,
                         @RequestParam(required = false) Double timeoutmoney,
                         @RequestParam(required = false) Double ownermoney,
                         @RequestParam(required = false) Double discount,
                         @RequestParam(required = false) Integer carstate){

        Cars cars=new Cars(carid,carstate);
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        System.out.println(dateFormat.format(date));
        CarDevice carDevice=new CarDevice(carid,deviceid,simcard,date);
        CarPrice carPrice=new CarPrice(carid,dayprice,tempmoney,rulemoney,
                basicinsure,timeoutmoney,ownermoney,discount,date);
        carDeviceService.insert(carDevice);
        carPriceService.insert(carPrice);
        carShareService.updateByPrimaryKeySelective(cars);
        return new Result();
    }
}
